/**
 * @name 爱范儿
 * @appid 2349521
 */

import Creeper, { _ } from '../lib/creeper';

const app = new Creeper({
  domains: ['www.ifanr.com'],
  scanUrls: ['https://www.ifanr.com/'],
  contentUrlRegexes: [/https:\/\/www.ifanr.com\/(\w+\/)?\d+/], // 内容页url正则
  fields: [
    {
      name: 'content',
      alias: '内容',
      selector: `//article[contains(@class, 'c-article-content')]`,
      required: true,
    },
  ],
});

app.onProcessScanPage((page, content, site) => {
  const re = /background-image:url\('([^']+)(?:[^=]+=){3}"([^"]+)"[^>]+>([^<]+)[^-]+-summary">([^<]+)[^_]+[^=]+="(\d+)/g;
  let iCount = 0;

  content.replace(re, (m, img, url, title, lead, time) => {
    if (''.indexOf('/video') > -1) {
      return m; // 忽略视频频道
    }

    const dt = new Date(time * 1000);

    if (_.expired(dt, '24h')) {
      return m; // 丢弃超过24小时的新闻
    }

    site.addUrl(url); // 添加内容列表
    _.store.set(url, {
      title,
      lead,
      time,
      image: _.fixImg(img, false),
    });

    iCount++;

    return m;
  });

  console.log(`发现 ${iCount} 个符合要求的文章，准备爬取...`);

  return false;
});

app.start();
